package com.project.common.tools;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

/**
 * 分页处理
 *
 * @author ysy
 * @date 2022-09-01
 */
public class PageUtil {

    /**
     * @param page
     * @param pageSize
     * @return
     */
    static public PageRequest getPage(Integer page, Integer pageSize) {
        return getPage(page, pageSize, null);
    }

    /**
     * @param page
     * @param pageSize
     * @param sort
     * @return
     */
    static public PageRequest getPage(Integer page, Integer pageSize, Sort sort) {
        if (page < 0) {
            throw new IllegalArgumentException("Page index must not be less than zero!");
        }
        if (pageSize < 1) {
            throw new IllegalArgumentException("Page size must not be less than one!");
        }
        if (null != sort) {
            return PageRequest.of(page == 0 ? page : page - 1, pageSize, sort);
        }
        return PageRequest.of(page == 0 ? page : page - 1, pageSize);
    }
}
